Repository navigation
Reject card numbers of impossible length in card_number() - #594
Open
yuchengshen931122-creator wants to merge 1 commit into
Open
yuchengshen931122-creator wants to merge 1 commit into
yuchengshen931122-creator wants to merge 1 commit into
Conversation
card_number() only computed the Luhn checksum, so any string of digits with a
matching check digit was a valid card number, whatever its length:
card_number("0") # True
card_number("18") # True
card_number("62123456789012345676") # True (20 digits)
A card number has at most 19 digits and, made of an issuer identification
number, an account number and a check digit, at least 8. Numbers outside that
range are now rejected before the checksum. The brand validators already
check their own length and are not affected.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
mjanthony819
left a comment
There was a problem hiding this comment.
Verified locally against this branch (b0d62fd): the full card suite (tests/test_card.py, 137 tests) and the card.py doctests both pass, and the boundary behavior matches the diff's intent.
- Luhn-valid 8-digit and 19-digit values still pass ("40128886", "6212345678901234569").
- The previously-True cases from the issue ("18", "0", and the 20-digit "62123456789012345676") are now rejected; on master all three return True.
- The only inputs that change result are all-digit strings outside 8-19, which is the point of the change. Values with separators or other non-digit characters still fail in the existing parse path, same as before, so the character counting in len() introduces no gap.
- Non-string input degrades the same way on both branches: the decorator wraps the TypeError into a ValidationError (only the reason string differs).
- visa/mastercard/unionpay (16 digits) and amex (15) call card_number() first, so they inherit the gate; their own stricter length checks run afterwards, so no brand validator is affected.
One observation, not a request: the floor of 8 matches the pre-2017 ISO/IEC 7812 layout (6-digit IIN plus account digit plus check digit). The 2017 revision's 8-digit IIN would imply a floor of 10, but 8 is the defensible minimum for cards in circulation and fits the issue's description.
AI assistance covered the analysis and this review text.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
card_number()only computes the Luhn checksum, so any string of digits with a matching check digit is a valid card number, whatever its length:card_number("0"),card_number("18")and the 20-digitcard_number("62123456789012345676")are allTrue.A card number has at most 19 digits and, made of an issuer identification number, an account number and a check digit, at least 8. Numbers outside that range are now rejected before the checksum. The brand validators already check their own length and are not affected.
About the bounds: 19 is the maximum that ISO/IEC 7812-1 is commonly cited for, and it agrees with the 19-digit Visa numbers of #556. For the minimum we could not read the standard itself; 8 is the loosest reading of the public summaries (the 2017 edition is said to require 10, and the large networks issue 12 or more). We chose the loosest value so that nothing plausible is rejected. If you prefer 10 or 12, it is one number in the code and one test value.
This is separate from #567 / #568, which are about non-ASCII digits in the same function; the branch merges cleanly with #568, #558 and #569.
The added tests (9 cases, each value Luhn-valid so that only the length decides) include 7 that fail on master. The full suite including doctests passes on Python 3.13 (1,583 passed); Ruff lint and format, Pyright and Bandit report nothing. Other Python versions are left to CI.
Found while writing specification-based tests for a university software-testing course project (NYCU). Prepared with AI assistance; reproduced and tested locally.
🤖 Generated with Claude Code